package com.bookmanger.controller;

import cn.hutool.core.bean.BeanUtil;
import com.bookmanger.common.result.Result;
import com.bookmanger.entity.User;
import com.bookmanger.entity.dto.LoginDTO;
import com.bookmanger.entity.request.LoginRequest;
import com.bookmanger.service.LoginService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @folder 用户登录
 * @author huangfuhao
 * @Date 2024/11/4
 */

@RestController
@RequestMapping("/login")
public class LoginController {

    private LoginService loginService;

    public LoginController(LoginService loginService){
        this.loginService = loginService;
    }

    /**
     * 注册
     * @param loginRequest
     * @return
     */
    @PostMapping("/register")
    private Result<LoginDTO> register(@RequestBody LoginRequest loginRequest){
        return loginService.register(BeanUtil.copyProperties(loginRequest, User.class));
    }

    /**
     * 登录
     * @param loginRequest
     * @return
     */
    @PostMapping("/login")
    private Result<LoginDTO> login(@RequestBody LoginRequest loginRequest){
        return loginService.login(BeanUtil.copyProperties(loginRequest, User.class));
    }
}
